﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <qfiledialog.h>
#include <QDebug>

QVector<double> datafromstring(QString str)
{
    QVector<double> vec;
    QStringList list=str.split(" ");
    for(int i=0;i<list.size();++i)
    {
        QString dstr=list.at(i);
        dstr=dstr.trimmed();
        if(dstr.isEmpty())
            continue;
        double t=dstr.toDouble();
        vec.push_back(t);
    }
    return vec;
}
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initforms();
    cal=new Caculaer();
    calfinished=false;
}

MainWindow::~MainWindow()
{
    delete ui;
    delete cal;
}


void MainWindow::on_pushButton_caculate_clicked()
{
    if(ui->tableWidget->rowCount() < 13)
    {
        QMessageBox::warning(this,"warning",QString::fromLocal8Bit("数据样本数量不足！"));
        return;
    }
    QMap<int, Freme> map;
    for(int i=0;i<ui->tableWidget->rowCount();++i)
    {
        int indx=ui->tableWidget->item(i,0)->text().toInt();

        QString data= ui->tableWidget->item(i,1)->text();
        Freme frame;
        frame.pos = datafromstring(data);
        QString imag=ui->tableWidget->item(i,2)->text();
        frame.imagepath=imag;
        map[indx]=frame;
    }
    cal->setdatasouce(sourcepath);
    if(cal->execute(map))
    {
        calfinished=true;
        QMessageBox::information(this,"success","Calculate successful!");
        showresult();
    }
    else
    {
        QMessageBox::information(this,"failure","Calculate failure: some error in data source!");
        ui->textEdit->clear();
    }

}


void MainWindow::on_pushButton_import_clicked()
{
    QString path= qApp->applicationDirPath();
    path.append("/../sourcedate");
    qDebug()<<path;
    QString filename= QFileDialog::getOpenFileName(this,"open the config text",path,"*.txt");
    qDebug()<<filename;
    if(filename.isEmpty())
        return;
    QFile file(filename);

    if(!file.exists())
    {
        LOG_ERROR("FILE CAN NOT FOUND!");
        return;
    }


    QString sour;
    if(file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        sourcepath = filename.left(filename.lastIndexOf("/"));
        sour=file.readAll();
    }
    else
    {
        LOG_ERROR("FILE CAN NOT OPEN!");
        return;
    }
    updatesource(sour);
    file.close();
}

void MainWindow::initforms()
{
    ui->tableWidget->setColumnWidth(0,40);
    ui->tableWidget->setColumnWidth(1,300);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);//禁止修改
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//整行选中的方式

    ui->spinBox_row->setValue(cornerRow);
    ui->spinBox_col->setValue(cornerColum);
    ui->doubleSpinBox_dist->setValue(patternLength);
    ui->spinBox_imgh->setValue(imageht);
    ui->spinBox_imgw->setValue(imagewd);
}

void MainWindow::showresult()
{
    if(!calfinished)
        return;
    if(ui->radioButton_rpy->isChecked())
    {
        QVector<double> pos= cal->geteulerreslut();
        QString str;
        for(int i=0;i<pos.size();++i)
        {
            str.append(QString::number(pos.at(i))).append(" ");
        }
        ui->textEdit->setText(str);
    }
    else if(ui->radioButton_matrix->isChecked())
    {
        QMatrix4x4 mat = cal->getmatrixresult();
        QString matstr;
        //qDebug()<<mat;
        for(int i=0;i<4;++i)
        {
            for(int j=0;j<4;++j)
            {
                matstr.append(QString::number(mat(i,j))).append(" ");
            }
            matstr.append("\n");
        }
        ui->textEdit->setText(matstr);
    }
    else if(ui->radioButton_fourelem->isChecked())
    {
        QVector<double> pos= cal->getfourelementresult();
        QString str;
        for(int i=0;i<pos.size();++i)
        {
            str.append(QString::number(pos.at(i))).append(" ");
        }
        ui->textEdit->setText(str);
    }
}

void MainWindow::updatesource(QString sour)
{
    ui->tableWidget->clearContents();
    if(sour.isEmpty())
    {
        return;
    }
    QStringList list=sour.split("\n");
    //ui->tableWidget->setRowCount(list.size());
    foreach (QString str, list) {
        str=str.trimmed();
        if(str.isEmpty())
            continue;
        int i = str.indexOf(",");
        QString robstr = str.left(i);
        QString imgstr = str.right(str.length()-i-1);
//        if(imgstr.indexOf("/")<0)
//        {
//            imgstr= sourcepath+"/"+imgstr;
//        }
        qDebug()<<"robstr == "<<robstr;
        qDebug()<<"imgstr == "<<imgstr;

        int t=ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(t);
        ui->tableWidget->setItem(t,0,new QTableWidgetItem(QString::number(t)));
        ui->tableWidget->setItem(t,1,new QTableWidgetItem(robstr));
        ui->tableWidget->setItem(t,2,new QTableWidgetItem(imgstr));
    }
}


void MainWindow::on_spinBox_row_valueChanged(int arg1)
{
    cornerRow=arg1;
}


void MainWindow::on_spinBox_col_valueChanged(int arg1)
{
    cornerColum=arg1;
}

void MainWindow::on_doubleSpinBox_dist_valueChanged(double arg1)
{
    patternLength=arg1;
}


void MainWindow::on_pushButton_clear_clicked()
{
    ui->tableWidget->clearContents();
    calfinished=false;
    cal->clear();
}


void MainWindow::on_spinBox_imgw_valueChanged(int arg1)
{
    imagewd=arg1;
}


void MainWindow::on_spinBox_imgh_valueChanged(int arg1)
{
    imageht=arg1;
}


void MainWindow::on_radioButton_rpy_clicked()
{
    showresult();
}


void MainWindow::on_radioButton_fourelem_clicked()
{
    showresult();
}


void MainWindow::on_radioButton_matrix_clicked()
{
    showresult();
}

